package com.webmall.servlet.user;

import com.webmall.entity.UserEntity;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

import static com.webmall.service.webmall_UserDao.*;

@WebServlet("/manageHtml/admin_doUserModify")
public class doUserModify extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //设置字符集
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");

        //根据add界面的name参数进行内容获取
        String username = request.getParameter("userName");
        String name = request.getParameter("name");
        String pwd = request.getParameter("pwd");
        String sex = request.getParameter("sex");
        String birthday = request.getParameter("birthday");
        String email = request.getParameter("email");
        String mobile = request.getParameter("mobile");
        String address = request.getParameter("address");
        //状态码与id在提交表单时 添加隐藏的参数
        String status = request.getParameter("userStatus");
        String cpage =request.getParameter("cpage");

        int user_status =0;
        if (null!= status){
            user_status = Integer.parseInt(status);


        }
        //创建用户书体
        //默认添加用户身份为1 普通用户
        UserEntity user = new UserEntity
                (username,name,pwd,sex,birthday,null,email,mobile,address,user_status);


        //调用service里的dao
      int count= updateById(user);





        //成功或失败重定位到哪儿
     if (count >0){
            response.sendRedirect(request.getContextPath()+"/manageHtml/admin_doUserShow?cp="+request.getParameter("cpage"));//修改成功 返回到当前页
//            PrintWriter out = response.getWriter();
//            out.write("<script>");
//            out.write("alert('用户添加成功')");
//            out.write("location.href='manageHtml/admin_useradd.jsp'");
//            out.write("</script>");

        }else {//失败 用输出流输出 js语句
            PrintWriter out = response.getWriter();
            out.write("<script>");
            out.write("alert('用户修改失败')");
            out.write("location.href=<%=request.getContextPath()%>'/manageHtml/admin_toUserUpdate？id="+username+"'");
            out.write("</script>");

        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
